package testsuite_Login;

import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

import pom.Homepage;
import base.BaseScript;

public class Login extends BaseScript {
        private void wait2() {
                try {
                        Thread.sleep(2000);
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
        }

        @AfterMethod
        public void backToHomepage() {
                driver.get("http://www.bigtester.com");
                wait2();
        }

        //@Test(priority = 1)
        public void falseLoginBetterCodeExampleForPOM() {
                String username = "bigtester";
                String password = "bigtester";
                Reporter.log("test case0001-false login page factory example: ");
                try {
                        Homepage nlHomepage = new Homepage(driver);

                        nlHomepage.getLoginForm().typeUsername(username);
                        wait2();

                        nlHomepage.getLoginForm().typePassword(password);
                        wait2();

                        nlHomepage.getLoginForm().submitFalseLogin();

                } catch (AssertionError E) {
                        throw new IllegalStateException(E.getMessage() + " in Test case 'false login better example'");
                }

        }

        @Test(priority = 1)
        public void TrueLoginBetterCodeExampleForPOM() {
                String username = "jasonbigtester";
                String password = "jasonbigtester";
                Reporter.log("test case0000-True login page factory example: ");
                try {
                        Homepage nlHomepage = new Homepage(driver);

                        nlHomepage.getLoginForm().typeUsername(username);
                        wait2();

                        nlHomepage.getLoginForm().typePassword(password);
                        wait2();

                        nlHomepage.getLoginForm().submitLogin();

                } catch (AssertionError E) {
                        throw new IllegalStateException(E.getMessage() + " in Test case 'false login better example'");
                }

        }
        //@Test(priority = 3)
        public void falseLoginPageFactoryExample() {
                String username = "bigtester";
                String password = "bigtester";
                Reporter.log("test case0003-false login page factory example: ");

                Reporter.log("homepage step start->");
                Homepage nlHomepage = new Homepage(driver);
                Reporter.log("homepage step end->");

                Reporter.log("login username type start->");
                nlHomepage.getPFLoginForm().typeUsername(username);
                Reporter.log("login username type end->");

                wait2();

                Reporter.log("login passwork type start->");
                nlHomepage.getPFLoginForm().typePassword(password);
                Reporter.log("login passwork type end->");

                wait2();

                Reporter.log("login submit button click start->");
                nlHomepage.getPFLoginForm().submitFalseLogin();
                Reporter.log("login submit button click end->");

        }

        //@Test(priority = 2)
        public void flaseLoginBadCodeExampleForPOM() {
                Reporter.log("test case0002-false login page factory example: ");
                String username = "bigtester";
                String password = "bigtester";
                By usernameLocator = By.id("modlgn_username");
                By passwordLocator = By.id("modlgn_passwd");
                By loginButtonLocator = By.name("Submit");
                driver.findElement(usernameLocator).sendKeys(username);
                wait2();
                driver.findElement(passwordLocator).sendKeys(password);
                wait2();
                driver.findElement(loginButtonLocator).click();
                Assert.assertTrue(
                                driver.getPageSource().contains("Username and password do not match or you do not have an account yet1"),
                                "False Login page not appear");

        }

        @BeforeTest
        public void initTest() {
                super.initTest();
        } 

        @AfterTest
        public void finalizeTest() {
                super.finalizeTest();
        }

}

